//
//  SCHomeController.m
//  shangcheng
//
//  Created by 张艳江 on 2019/5/28.
//  Copyright © 2019 张艳江. All rights reserved.
//

#import "SCHomeController.h"
#import "SCNavigationController.h"
#import "SDCycleScrollView.h"
#import "SCHomeModel.h"
#import "SCHomeClassifyView.h"
#import "SCHomeCell.h"

#import "SCShopMallController.h"
#import "SCEatPlayController.h"
#import "SCHotelHomeController.h"

CGFloat const k_home_lr_space = 10; // 间距
CGFloat const k_home_tb_space = 8; // 间距
CGFloat const k_home_table_header_h = 44;

#define k_cycle_scrollView_h     kScreenW*188/375
#define k_table_header_view_h    k_cycle_scrollView_h + k_classify_view_h


@interface SCHomeController ()<UISearchBarDelegate,UITableViewDelegate,UITableViewDataSource,SDCycleScrollViewDelegate,SCHomeClassifyViewDelegate>

@property (nonatomic , strong) UIButton *address_button;
@property (nonatomic , strong) UILabel *address_label;
@property (nonatomic , strong) NSString *address_text;

@property (nonatomic , strong) UISearchBar *search_bar;
@property (nonatomic , strong) NSString *search_bar_placeholder;

@property (nonatomic , strong) UIButton *chat_button;

@property (nonatomic , strong) UITableView *table_view;

@property (nonatomic , strong) SDCycleScrollView  *cycle_scrollView;// 轮播图
@property (nonatomic , strong) SCHomeClassifyView *classify_view;
@property (nonatomic , strong) NSArray *classify_array;
@property (nonatomic , strong) NSMutableArray *bmsh_array;


@end

@implementation SCHomeController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.search_bar_placeholder = [NSString string];
    self.search_bar_placeholder = @"资生堂面膜";
    
    self.address_text = [NSString string];
    self.address_text = @"北京";
    
    
    NSArray *array = @[@{@"classify_id":@"0",@"classify_img":@"home_btn_sc",@"classify_name":@"商城"},@{@"classify_id":@"1",@"classify_img":@"home_btn_ms",@"classify_name":@"美食"},@{@"classify_id":@"2",@"classify_img":@"home_btn_wm",@"classify_name":@"外卖"},@{@"classify_id":@"3",@"classify_img":@"home_btn_jdzs",@"classify_name":@"酒店住宿"},@{@"classify_id":@"4",@"classify_img":@"home_btn_shfw",@"classify_name":@"生活服务"},@{@"classify_id":@"5",@"classify_img":@"home_btn_xxyl",@"classify_name":@"休闲娱乐"},@{@"classify_id":@"6",@"classify_img":@"home_btn_spwz",@"classify_name":@"视频问诊"},@{@"classify_id":@"7",@"classify_img":@"home_btn_aqcx",@"classify_name":@"安全出行"}];
    
    self.classify_array = [SCHomeClassifyModel mj_objectArrayWithKeyValuesArray:array];
    
    self.bmsh_array = [[NSMutableArray alloc] init];
    
    [self createUI];
    [self loadHomeData];
}

#pragma mark 私有方法 -- UI
-(void)createUI{
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kNavBarHeight)];
    UIColor *color =  self.navigationController.navigationBar.barTintColor;
    [view setBackgroundColor:color];
    
    UIImageView *address_img = [[UIImageView alloc] init];
    address_img.image = [UIImage imageNamed:@"nav_icon_adress"];
    [self.address_button addSubview:address_img];
    [self.address_button addSubview:self.address_label];
    
    UIImageView *chat_img = [[UIImageView alloc] init];
    chat_img.image = [UIImage imageNamed:@"nav_btn_xx"];
    [self.chat_button addSubview:chat_img];
    
    [view addSubview:self.address_button];
    [view addSubview:self.search_bar];
    [view addSubview:self.chat_button];
    
    CGSize size = [self.address_label.text sizeWithAttributes:@{NSFontAttributeName:self.address_label.font}];
    
    CGFloat address_img_w = 17;
    CGFloat address_button_w = size.width + address_img_w + k_home_lr_space;
    
    [self.address_button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.left.and.bottom.mas_equalTo(0);
        make.right.mas_equalTo(self.address_label.mas_right).mas_offset(k_home_lr_space);
    }];
    
    [address_img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.bottom.mas_equalTo(-13);
        make.width.and.height.mas_equalTo(17);
    }];
    
    [self.address_label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(address_img.mas_right);
        make.bottom.mas_equalTo(address_img.mas_bottom);
        make.height.mas_equalTo(address_img.mas_height);
        make.width.mas_equalTo(size.width + 5);
    }];
    
    CGFloat chat_button_w = 38;
    CGFloat search_w = kScreenW - address_button_w - chat_button_w - 10;
    
    [self.search_bar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.address_button.mas_right);
        make.top.mas_equalTo(0);
        make.height.mas_equalTo(29);
        make.width.mas_equalTo(search_w);
    }];
    
    [self.chat_button mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.search_bar.mas_right);
        make.width.mas_equalTo(chat_button_w);
        make.top.and.bottom.mas_equalTo(0);
        make.right.mas_equalTo(0);
    }];
    
    [chat_img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(k_home_lr_space);
        make.width.and.height.mas_equalTo(17);
        make.bottom.mas_equalTo(-13);;
    }];
    
    [self.navigationItem.titleView sizeToFit];
    self.navigationItem.titleView = view;
    
    [self.view addSubview:self.table_view];
    
}

-(UIView *)tabelHeaderViewInTopSection{
    
    UIView *header_view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, k_table_header_view_h)];
    [header_view addSubview:self.cycle_scrollView];
    [header_view addSubview:self.classify_view];
    
    return header_view;
    
}
#pragma mark 私有方法 --请求数据
- (void)loadHomeData{
    
    NSString *url = [MyTool splicingWithUrl:@"admin.php?g=System&c=GoodsApp&a=index"];
    //NSLog(@"商城首页===%@",url);
    [SVProgressHUD showWithStatus:@"加载中..."];
    [[NetworkRequestTool sharedNetworkTool] GET:url parameters:nil finished:^(id result, NSString *error) {
        //NSLog(@"商城首页返回数据:%@",result);
        [SVProgressHUD dismiss];
        
        //        self.model = [SCModel mj_objectWithKeyValues:result[@"data"]];
        //        self.model.lun = [SCModel mj_objectArrayWithKeyValuesArray:self.model.lun];
        //        self.model.recommend = [SCModel mj_objectArrayWithKeyValuesArray:self.model.recommend];
        //        self.model.fenlei = [SCModel mj_objectArrayWithKeyValuesArray:self.model.fenlei];
        //        for (SCModel *model in self.model.fenlei) {
        //            model.list = [SCModel mj_objectArrayWithKeyValuesArray:model.list];
        //        }
        //        self.sectionDataArray = [NSMutableArray array];
        //        if (self.model.recommend.count != 0) {
        //            SCModel *model = [[SCModel alloc]init];
        //            model.cat_id = @"0";
        //            model.cat_name = @"为您推荐";
        //            model.list = [self.model.recommend copy];
        //            [self.sectionDataArray addObject:model];
        //        }
        //        [self.sectionDataArray addObjectsFromArray:self.model.fenlei];
        //
        //        NSMutableArray *scrollImages = [NSMutableArray array];
        //        for (SCModel *lunModel in self.model.lun) {
        //            [scrollImages addObject:[NSString stringWithFormat:@"%@%@",Lun_ImageURL,lunModel.bg_pic]];
        //        }
        //        self.cycleScrollView.imageURLStringsGroup = [scrollImages copy];
        //        [self.tableView reloadData];
    }];
}

#pragma mark UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        return 1;
    }else if (section == 2 || section == 3 || section == 1 ){
        return 2;
    }else if (section == 4){
        return 1;
    }
    
    return 0;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifity1;
    
    if (indexPath.section == 0) {
        
        identifity1 = @"SCHomeBMSHViewCell";
        SCHomeCell *cell = [tableView dequeueReusableCellWithIdentifier:identifity1];
        if (cell == nil) {
            cell = [[SCHomeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifity1 ];
        }
        
        cell.selectionStyle  = UITableViewCellSelectionStyleNone;
        [cell setHome_bmsh_array:self.bmsh_array];
        return cell;
    }else if (indexPath.section == 1) {
        
        identifity1 = @"SCHomeFJMSViewCell";
        SCHomeCell *cell = [tableView dequeueReusableCellWithIdentifier:identifity1];
        if (cell == nil) {
            cell = [[SCHomeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifity1 ];
        }
        
        cell.selectionStyle  = UITableViewCellSelectionStyleNone;
        
        return cell;
    }else if (indexPath.section == 2) {
        
        identifity1 = @"SCHomeJXJDViewCell";
        SCHomeCell *cell = [tableView dequeueReusableCellWithIdentifier:identifity1];
        if (cell == nil) {
            cell = [[SCHomeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifity1 ];
        }
        
        cell.selectionStyle  = UITableViewCellSelectionStyleNone;
        
        return cell;
    }else if (indexPath.section == 3) {
        
        identifity1 = @"SCHomeXXYLViewCell";
        SCHomeCell *cell = [tableView dequeueReusableCellWithIdentifier:identifity1];
        if (cell == nil) {
            cell = [[SCHomeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifity1 ];
        }
        
        cell.selectionStyle  = UITableViewCellSelectionStyleNone;
        
        return cell;
    }else if (indexPath.section == 4) {
        
        identifity1 = @"SCHomeJFSCViewCell";
        SCHomeCell *cell = [tableView dequeueReusableCellWithIdentifier:identifity1];
        if (cell == nil) {
            cell = [[SCHomeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifity1 ];
        }
        
        cell.selectionStyle  = UITableViewCellSelectionStyleNone;
        
        return cell;
    }
    
    
    
    
    
    identifity1 = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifity1];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifity1 ];
    }
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        return kScreenW*92/375;
        
    }else if (indexPath.section == 1){
        
        return k_fjms_cell_h;
        
    }else if (indexPath.section == 2) {
        
        return k_jxjd_cell_h;
        
    }else if (indexPath.section == 3) {
        
        return k_xxyl_cell_h;
        
    }else if (indexPath.section == 4) {
        
        return k_jfsc_cell_h;
        
    }
    
    return 87;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return k_home_table_header_h;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return .1;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *headerV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, k_home_table_header_h)];
    headerV.backgroundColor =  RGB(244, 244, 244);
    
    UIImageView *image_view = [[UIImageView alloc] init];
    [headerV addSubview:image_view];
    
    UILabel *title_label = [[UILabel alloc] init];
    title_label.textColor = RGB(51, 51, 51);
    title_label.font = [UIFont systemFontOfSize:15.0f];
    title_label.textAlignment = NSTextAlignmentLeft;
    [headerV addSubview:title_label];
    
    UIButton *title_r_button = [[UIButton alloc] init];
    title_r_button.backgroundColor = [UIColor clearColor];
    title_r_button.tag = section;
    [title_r_button addTarget:self action:@selector(clickHeaderRightButton:) forControlEvents:UIControlEventTouchUpInside];
    [headerV addSubview:title_r_button];
    
    UILabel *title_r_label = [[UILabel alloc] init];
    title_r_label.text = @"查看更多 >";
    title_r_label.textColor = RGB(142, 142, 142);
    title_r_label.font = [UIFont systemFontOfSize:11.0f];
    title_r_label.textAlignment = NSTextAlignmentRight;
    [title_r_button addSubview:title_r_label];
    
    [image_view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(9);
        make.width.and.height.mas_equalTo(16);
        make.top.mas_equalTo(15);
    }];
    
    [title_label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(image_view.mas_right).mas_offset(4);
        make.height.mas_equalTo(16);
        make.top.mas_equalTo(15);
        make.right.mas_equalTo(title_r_button.mas_left);
    }];
    
    [title_r_button mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.and.bottom.mas_equalTo(0);
        make.right.mas_equalTo(-9);
        make.width.mas_equalTo(100);
    }];
    
    [title_r_label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.mas_equalTo(0);
        make.height.mas_equalTo(11);
        make.top.mas_equalTo(18);
        
    }];
    
    if (section == 0) {
        
        image_view.image = [UIImage imageNamed:@"home_icon_bmsh"];
        title_label.text = @"便民生活";
        
    }else if (section == 1){
        
        image_view.image = [UIImage imageNamed:@"home_icon_fjms"];
        title_label.text = @"附近美食";
        
    }else if (section == 2) {
        
        image_view.image = [UIImage imageNamed:@"home_icon_jxjd"];
        title_label.text = @"精选酒店";
        
    }else if (section == 3) {
        
        image_view.image = [UIImage imageNamed:@"home_icon_xxyl"];
        title_label.text = @"休闲娱乐";
        
    }else if (section == 4) {
        
        image_view.image = [UIImage imageNamed:@"home_icon_jfsc"];
        title_label.text = @"福豆商城";
        
    }
    
    return headerV;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}

#pragma mark - 广告轮播图
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    
}
#pragma mark - 点击8个板块
- (void)clickHomeClassifyWithIndex:(NSInteger)index{
    if (index == 0) {//商城
        SCShopMallController *vc = [[SCShopMallController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (index == 1){
        
    }else if (index == 2){
        
    }else if (index == 3){
        SCHotelHomeController *vc = [[SCHotelHomeController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (index == 4){
        
    }else if (index == 5){//吃喝玩乐
        SCEatPlayController *vc = [[SCEatPlayController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (index == 6){
        
    }else if (index == 7){
        
    }
}

#pragma mark 私有方法 -- 初始化
-(UIButton *)address_button{
    
    if (_address_button == nil) {
        _address_button = [[UIButton alloc] init];
        _address_button.backgroundColor = [UIColor clearColor];
        
    }
    return _address_button;
    
}

-(UILabel *)address_label{
    
    if (_address_label == nil) {
        _address_label = [[UILabel alloc] init];
        _address_label.text = self.address_text;
        _address_label.textColor = RGB(51, 51, 51);
        _address_label.font = [UIFont systemFontOfSize:14.0f];
    }
    return _address_label;
}

-(UISearchBar *)search_bar{
    
    if (_search_bar == nil) {
        
        _search_bar = [[UISearchBar alloc] init];
        _search_bar.backgroundImage = [[UIImage alloc] init];
        _search_bar.showsCancelButton = NO;
        _search_bar.backgroundColor =  RGB(244, 244, 244);
        _search_bar.translucent  = YES;
        _search_bar.delegate     = self;
        _search_bar.placeholder  = self.search_bar_placeholder;
        _search_bar.keyboardType = UIKeyboardTypeDefault;
        [_search_bar setBarTintColor:RGB(244, 244, 244)];
        
        UIImage *searchIcon = [UIImage imageNamed:@"nav_icon_search"];
        [_search_bar setImage:searchIcon forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
        
        for (UIView* subview in [[_search_bar.subviews lastObject] subviews]) {
            
            if ([subview isKindOfClass:[UITextField class]]) {
                UITextField *textField = (UITextField*)subview;
                //修改输入字体的颜色
                textField.textColor = [UIColor blackColor];
                textField.font = [UIFont systemFontOfSize:14.0f];
                //修改输入框的颜色
                [textField setBackgroundColor:RGB(244, 244, 244)];
                
            } else if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
            {
                [subview removeFromSuperview];
            }
        }
        
    }
    return _search_bar;
    
}

-(UIButton *)chat_button{
    
    if (_chat_button == nil) {
        _chat_button = [[UIButton alloc] init];
        _chat_button.backgroundColor = [UIColor clearColor];
    }
    return _chat_button;
}

-(UITableView *)table_view{
    
    if (_table_view == nil) {
        _table_view = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH - kTabBarHeight - kTopHeight)];
        _table_view.separatorStyle = UITableViewCellSeparatorStyleNone;
        _table_view.delegate = self;
        _table_view.dataSource = self;
        _table_view.backgroundColor = RGB(244, 244, 244);
        [_table_view setTableHeaderView:[self tabelHeaderViewInTopSection]];
        
    }
    return _table_view;
}

-(SDCycleScrollView *)cycle_scrollView{
    
    if (_cycle_scrollView == nil) {
        
        _cycle_scrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, kScreenW, k_cycle_scrollView_h) delegate:self placeholderImage:[UIImage imageWithColor:BGColor]];
        _cycle_scrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
        _cycle_scrollView.currentPageDotColor = RGB(251, 30, 72);
        _cycle_scrollView.pageDotColor = [UIColor whiteColor];
        _cycle_scrollView.autoScrollTimeInterval = 3.0f;
        _cycle_scrollView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
    }
    return _cycle_scrollView;
    
}

-(SCHomeClassifyView *)classify_view{
    
    if (_classify_view == nil) {
        _classify_view = [[SCHomeClassifyView alloc] initWithFrame:CGRectMake(0, k_cycle_scrollView_h, kScreenW, k_classify_view_h)];
        _classify_view.backgroundColor = [UIColor whiteColor];
        _classify_view.home_classify_array = [self.classify_array mutableCopy];
        _classify_view.delegate = self;
    }
    return _classify_view;
}





@end
